/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tlf.dyjc5.listener;

import com.tlf.dyjc5.service.LogService;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Web application lifecycle listener.
 *
 * @author tengyu
 */
public class SessionServletListener implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent se) {        
        //ServletContext context = se.getSession().getServletContext();
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(se.getSession().getServletContext());
        LogService logService = (LogService) context.getBean("logService");
        
        HttpSession session = se.getSession();
        Map vipList = (Map)session.getServletContext().getAttribute("vipList");
        Map userList = (Map)session.getServletContext().getAttribute("userList");       
        
        String user = (String)session.getAttribute("user");
        String orgCode= (String)session.getAttribute("orgCode");
        
        //session.getServletContext().removeAttribute(user);
        vipList.remove(user);
        userList.remove(user);
        
        long userId = (Long)session.getAttribute("userId");
        String clientIP = (String)session.getAttribute("IP");
        Integer exit = (Integer)session.getAttribute("exit");
        if(exit==null)
            logService.add(userId, "session超时退出登陆",clientIP,orgCode);
        else
            logService.add(userId, "退出登陆",clientIP,orgCode);
        //System.out.println(user +" session destroyed****");
    }
}
